import { addAccount } from '@/services/user/setting';
import { getUser } from '@/services/user/user';
import { message } from 'antd';

const UserSettingModel = {
  namespace: 'userSetting',
  state: {
    user: {},
    status: false,
  },
  effects: {
    *getUser({ payload }, { call, put }) {
      const response = yield call(getUser, payload);
      if (response.success) {
        yield put({
          type: 'saveUser',
          payload: response.data,
        });
        yield put({
          type: 'saveStatus',
          payload: true,
        });
      }
    },
    *addAccount({ payload }, { call, put }) {
      const response = yield call(addAccount, payload);
      if (response.success) {
        message.success('添加成功');
        yield put({
          type: 'saveStatus',
          payload: false,
        });
      } else {
        message.success(`添加失败 :${response.message}`);
      }
    },
  },
  reducers: {
    saveUser(state, action) {
      return { ...state, user: action.payload || {} };
    },
    saveStatus(state, action) {
      return { ...state, status: action.payload };
    },
  },
};
export default UserSettingModel;
